<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable class="!w-240px">
          <el-option label="用户" :value="1" />
          <el-option label="商户" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactMobile">
        <el-input
          v-model="queryParams.contactMobile"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.AUDIT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="用户信息" align="center" min-width="100px">
        <template #default="scope">
          <el-avatar :src="scope.row.user?.avatar || './avatar.png'" /><br />
          <el-tooltip :content="'用户编号：' + scope.row.userId" placement="top">
            <el-text>{{ scope.row.user?.nickname }}</el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="left" min-width="80px" show-overflow-tooltip>
        <template #default="scope">
          <div class="flex items-center">
            <el-tag type="primary">{{ scope.row.typeName }}</el-tag>
          </div>
          <div v-if="scope.row.type === 2" class="flex multi-hidden">
            <span>{{ scope.row.shop.name }}</span>
            <span v-if="scope.row.shop.fullAddress">（{{ scope.row.shop.fullAddress }}）</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="联系人信息" align="center" min-width="120px">
        <template #default="scope">
          <el-text>{{ scope.row.contactName || '--' }}</el-text>
          <br />
          <el-text>{{ scope.row.contactMobile || '--' }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" min-width="130px">
        <template #default="scope">
          <div>
            <dict-tag :type="DICT_TYPE.AUDIT_STATUS" :value="scope.row.auditStatus" />
            <br v-if="scope.row.auditStatus === 2" />
            <el-text v-if="scope.row.auditStatus === 2">原因：{{ scope.row.auditNote }}</el-text>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="left" min-width="250px">
        <template #default="scope">
          <el-text>申请时间：{{ formatDate(scope.row.createTime) || '--' }}</el-text>
          <br />
          <el-text>审核时间：{{ formatDate(scope.row.auditTime) || '--' }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="warning"
            @click="openAuditForm(scope.row.id)"
            v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
            :disabled="scope.row.auditStatus === 1"
          >
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 审核信息表单弹窗：修改 -->
  <AuditForm ref="auditFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { CloudAllianceApi } from '@/api/member/cloud-alliance/apply'
import AuditForm from './AuditForm.vue'
import { formatDate } from '@/utils/formatTime'

/** 申请详情 列表 */
defineOptions({ name: 'CloudAlianceApply' })

const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: undefined,
  contactMobile: undefined,
  auditStatus: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CloudAllianceApi.getPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const auditFormRef = ref()

const openAuditForm = (id: number | string) => {
  auditFormRef.value.open('update', id)
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
